package com.dongdong.bookstore.common.exception;

import com.dongdong.bookstore.common.api.IReturnCode;
import org.springframework.lang.Nullable;

/**
 * 自定义断言
 *
 * @author chiangkai
 * @date 2021-10-18
 * @email chiangkk@163.com
 */
public class Asserts {
    public static void fail(String message) {
        throw new ApiException(message);
    }

    public static void fail(IReturnCode code) {
        throw new ApiException(code);
    }

    public static void notNull(@Nullable Object object, String message) {
        if (object != null) {
            fail(message);
        }
    }

    public static void eq0(int count, String message) {
        if (count == 0) {
            fail(message);
        }
    }

    public static void gt0(int count, String message) {
        if (count > 0) {
            fail(message);
        }
    }


    public static void notNull(@Nullable Object object) {
        notNull(object, "断言异常-判断对象不能为空");
    }

    public static void isNull(@Nullable Object object,String message){
        if (object == null){
            fail(message);
        }
    }

    public static void isFalse(boolean bool,String message){
        if(!bool){
            fail(message);
        }
    }

    public static void isTrue(boolean bool,String message){
        if (bool){
            fail(message);
        }
    }


}
